<?php
header('Content-Type: text/html; charset=utf-8');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
require_once dirname(__FILE__) . '/../../../libs/phpMailer/class.phpmailer.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = MiscUtils::getParam('action', '');
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'send':
      send($myPdo);
      break;
    case 'add_to_cart':
      add_to_cart($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function send($myPdo) {
  $product_id = MiscUtils::getParam('product_track_id', '');
  $to = MiscUtils::getParam('to', '');
  $from = MiscUtils::getParam('from', '');
  $subject = MiscUtils::getParam('subject', '');
  $email = MiscUtils::getParam('email', '');
  $name = MiscUtils::getParam('name', '');
  $phone = MiscUtils::getParam('phone', '');
  $message = MiscUtils::getParam('message', '');
  unset($_SESSION[$product_id . 'email_err']);
  unset($_SESSION[$product_id . 'email']);
  unset($_SESSION[$product_id . 'name']);
  unset($_SESSION[$product_id . 'phone']);
  unset($_SESSION[$product_id . 'message']);
  
  $is_err = 0;
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $_SESSION[$product_id . 'email_err'] = 'Not a proper email';
    $is_err = 1;
  }
  
  if ($is_err == 1) {
    $_SESSION[$product_id . 'email'] = $email;
    $_SESSION[$product_id . 'name'] = $name;
    $_SESSION[$product_id . 'phone'] = $phone;
    $_SESSION[$product_id . 'message'] = $message;
    echo "
    <script>
    location.href = '" . preg_replace('/&at=(.*)/', '', $from) . "&at=1';
    </script>
    ";
    return;
  }
  
  $profile = new stdClass();
  $tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'user_profile', NULL, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmp) > 0) {
    $profile = $tmp[0];
  }
  try {
    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->Host = SMTP_HOST;
    $mailer->CharSet = "UTF-8";
    
    $content = '';
    
    $mailer->SMTPAuth = true;
    $mailer->Username = SMTP_USER_ATDN;
    $mailer->Password = SMTP_PASS_ATDN;
    $mailer->From = $email;
    $mailer->FromName = $name;
    
    $mailer->Body = "<html>
                       <body>
                         <table>
                           <tr>
                             <td>ID</td>
                             <td>" . MiscUtils::unique() . "</td>
                           </tr>
                           <tr>
                             <td>Subject</td>
                             <td>$subject</td>
                           </tr>
                           <tr>
                             <td>Email</td>
                             <td>$email</td>
                           </tr>
                           <tr>
                             <td>Name</td>
                             <td>$name</td>
                           </tr>
                           <tr>
                             <td>Phone</td>
                             <td>$phone</td>
                           </tr>
                           <tr>
                             <td>Message</td>
                             <td>$message</td>
                           </tr>
                         </table>
                       </body>
                     </html>";
    $mailer->Subject = $subject;
    $mailer->isHTML(true);
    $mailer->AddAddress($profile->email, '');
    $mailer->Send();
    echo "
    <script>
    location.href = '" . preg_replace('/&at=(.*)/', '', $to) . "&at=1';
    </script>
    ";
  } catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
  } catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
  }
}

function add_to_cart($myPdo) {
  $to = MiscUtils::getParam('to', '');
  $from = MiscUtils::getParam('from', '');
  $sel_attrs = json_decode(MiscUtils::getParam('sel_attrs', ''));
  $attr_opt_id = MiscUtils::getParam('attr_opt_id', '');
  $product_id = MiscUtils::getParam('product_track_id', '');
  $product = MiscUtils::getParam('product', '');
  $price = MiscUtils::getParam('price', '');
  $attr_opt_code = MiscUtils::getParam('attr_opt_code', '');
  $qty = MiscUtils::getParam('qty', '');
  $stock = MiscUtils::getParam('stock', '');
  unset($_SESSION[$product_id . 'cart_err']);
  unset($_SESSION[$product_id . 'sel_attrs']);
  unset($_SESSION[$product_id . 'qty']);

  $is_err = 0;
  if ($stock != '' && $qty > $stock) {
    $_SESSION[$product_id . 'cart_err'] = 'Out of stock';
    $is_err = 1;
  }
  
  $opts = explode('-', $attr_opt_id);
  if (count($opts) != count($sel_attrs) && count($sel_attrs) > 0) {
    if (isset($_SESSION[$product_id . 'cart_err'])) {
      $_SESSION[$product_id . 'cart_err'] .= ', Complete options';
    } else {
      $_SESSION[$product_id . 'cart_err'] = 'Complete options';
    }
    $is_err = 1;
  }
  
  $_SESSION[$product_id . 'sel_attrs'] = $sel_attrs;
  $_SESSION[$product_id . 'qty'] = $qty;
  
  if ($is_err == 1) {
    echo "
    <script>
    parent.location.href = '$from';
    </script>
    ";
    return;
  }
  
  if (isset($_COOKIE['my_cart'])) {
    $_SESSION['my_cart'] = json_decode($_COOKIE['my_cart']);
  }
  
  if (!isset($_SESSION['my_cart'])) {
    $_SESSION['my_cart'] = array();
  }
  
  $is_exist = false;
  foreach ($_SESSION['my_cart'] as $obj) {
    if ($obj->product_track_id == $product_id && $obj->attr_opt_id == $attr_opt_id) {
      $obj->price = $price;
      $obj->quantity += $qty;
      $obj->total = $obj->quantity * $price;
      $is_exist = true;
    }
  }
  
  if (!$is_exist) {
    $purchase = new stdClass();
    $purchase->product_track_id = $product_id;
    $purchase->customer_id = 0;
    $purchase->order_id = 0;
    $purchase->name = $product;
    $purchase->price = $price;
    $purchase->quantity = $qty;
    $purchase->total = $price * $qty;
    $purchase->attr_opt_id = $attr_opt_id;
    $purchase->attr_opt_code = $attr_opt_code;
    $_SESSION['my_cart'][] = $purchase;
  }
  
  setcookie('my_cart', json_encode($_SESSION['my_cart']), time() + 86400 * 7);
  
  unset($_SESSION[$product_id . 'qty']);
  
  echo "
  <script>
  parent.location.href = '$to';
  </script>
  ";
}
?>
